<?php

namespace builder\test\controllers;

use builder\base\BaseController;
use builder\helper\H5;
use builder\widget\ajax\BlockAjax;
use builder\widget\ajax\BlockModal;
use builder\widget\ajax\BlockPage;
use builder\widget\WidgetFacade;

class CompController extends BaseController
{
    public $actionVerbs = [
        'block' => ['GET'],
        'block-modal' => ['GET'],
        'block-ajax' => ['GET'],
        'block-page' => ['GET'],
        'url' => ['GET', 'POST'],
    ];
    public $publicActions = [
        'block',
        'block-modal',
        'block-ajax',
        'block-page',
        'url',
    ];

    public function actionBlock()
    {
        $blockArrays = [
            WidgetFacade::echo2Return(function () {
                //=-=-=-=-= modal
                BlockModal::begin([
                    'title' => 'Block Modal',
                    'url' => 'ymtest/comp/url',
                    'width' => '480px',
                    'height' => '580px',
                    'closeBtn' => 1,
                    'closeCallback' => 'console.log("This is modal-close callback. ")',
                    'options' => [
                        'class' => [
                            'd-inline-block',
                        ],
                    ],
                ]);
                echo '<button class="layui-btn layui-btn-sm layui-bg-light layui-border-theme m-5"><i class="bi bi-box-arrow-in-down-right"></i> 动态按钮(modal)</button>';
                BlockModal::end();
                echo "\n";

                //=-=-=-=-= ajax
                BlockAjax::begin([
                    'url' => 'ymtest/comp/url',
                    'ajaxSuccessCallback' => 'console.log("This is ajax returned success callback. ")',
                    'ajaxFailureCallback' => 'console.log("This is ajax returned failure callback. ")',
                    'options' => [
                        'class' => [
                            'd-inline-block',
                        ],
                    ],
                ]);
                echo '<button class="layui-btn layui-btn-sm layui-bg-light layui-border-theme m-5"><i class="bi bi-box-arrow-in-down-right"></i> 动态按钮(ajax)</button>';
                BlockAjax::end();
                echo "\n";

                //=-=-=-=-= page
                BlockPage::begin([
                    'url' => 'ymtest/comp/url',
                    'target' => '_blank',
                    'options' => [
                        'class' => [
                            'd-inline-block',
                        ],
                    ],
                ]);
                echo '<button class="layui-btn layui-btn-sm layui-bg-light layui-border-theme m-5"><i class="bi bi-box-arrow-in-down-right"></i> 动态按钮(page)</button>';
                BlockPage::end();
            }),
        ];

        return $this->renderContentX(
            implode("\n", $blockArrays),
            !!@$this->get['_partial']
        );
    }

    public function actionUrl()
    {
        if ($this->isAjax) {
            return $this->asOk();
        }

        return $this->renderContentX(
            H5::layuiStar([
                'value' => 2.5,
                'readonly' => true,
            ])
        );
    }

    //block（modal）示例
    public function actionBlockModal()
    {
        return $this->renderContent(
            WidgetFacade::echo2Return(function () {
                BlockModal::begin([
                    'url' => 'ymtest/comp/url',
                    'closeCallback' => 'console.log("This is modal-close callback. ")',
                ]);
                echo '<button class="layui-btn layui-btn-sm layui-bg-light layui-border-theme m-5"><i class="bi bi-box-arrow-in-down-right"></i> 动态按钮(modal)</button>';
                BlockModal::end();
            })
        );
    }

    //block（ajax）示例
    public function actionBlockAjax()
    {
        return $this->renderContent(
            WidgetFacade::echo2Return(function () {
                BlockAjax::begin([
                    'url' => 'ymtest/comp/url',
                    'ajaxSuccessCallback' => 'console.log("This is ajax returned success callback. ")',
                    'ajaxFailureCallback' => 'console.log("This is ajax returned failure callback. ")',
                ]);
                echo '<button class="layui-btn layui-btn-sm layui-bg-light layui-border-theme m-5"><i class="bi bi-box-arrow-in-down-right"></i> 动态按钮(ajax)</button>';
                BlockAjax::end();
            })
        );
    }

    //block（page）示例
    public function actionBlockPage()
    {
        return $this->renderContent(
            WidgetFacade::echo2Return(function () {
                BlockPage::begin([
                    'url' => 'ymtest/comp/url',
                    'target' => '_blank',
                ]);
                echo '<button class="layui-btn layui-btn-sm layui-bg-light layui-border-theme m-5"><i class="bi bi-box-arrow-in-down-right"></i> 动态按钮(page)</button>';
                BlockPage::end();
            })
        );
    }
}